﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class detail : System.Web.UI.Page
{
    protected BLL.LostAndFound bllLAF = new BLL.LostAndFound();
    protected List<MODEL.LostAndFound> model = new List<MODEL.LostAndFound>();
    protected StringBuilder sb = new StringBuilder();
    protected StringBuilder sb2 = new StringBuilder();
    protected BLL.Message bllmessage = new BLL.Message();
    protected MODEL.Users visitor;
    protected int tagvisitor;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!common.isLoginOrNot())
        {
            //无法留言 隐藏留言
            Reply.Visible = false;
            //可以抵消前台页面的js中的对象未实例化
            MODEL.Users user = new MODEL.Users();
            user.UName = "";
            visitor = user;
        }
        //uid = ((MODEL.Users)Session["currentUser"]).UId;
        else
        {
            //可以留言 显示留言
            Reply.Visible = true;
        }
            if (Request.QueryString["id"] != null)
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                //todo  id出现异常  为0或类型无法转换的处理或数据库中没有的id
                try
                {
                    //根据LId得到失物招领信息
                    model = bllLAF.getModelById(id);
                    if (model == null)
                    {
                        List<MODEL.LostAndFound> lafs = new List<MODEL.LostAndFound>();
                        MODEL.LostAndFound laf = new MODEL.LostAndFound();
                        laf.LId = 2;
                        lafs.Add(laf);
                        model = lafs;//抵消前台页面的错误
                        sb2.Append("没有任何失物招领信息");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    //todo导向错误页
                    
                    throw new Exception(ex.Message);
                }
                //model = bllLAF.getModelById(2);//抵消前台页面的错误
                if (model != null)
                {
                    if (model[0].LostProperty == "寻物启事")
                    {
                        sb.Append("失主");
                    }
                    else
                    {
                        sb.Append("拾者");
                    }
                    //todo:没有图片的处理
                    //if (string.IsNullOrWhiteSpace(model[0].Image))
                    //{
                    //    model[0].Image = "noimg.png";
                    //}

                    //绑定失物招领信息数据
                    rpDetail.DataSource = model;
                    rpDetail.DataBind();

                    if (Session["currentUser"] != null)
                    {
                        visitor = (MODEL.Users)Session["currentUser"];
                    }
                    //获取留言
                    int mlid = model[0].LId;
                    List<MODEL.Message> models = bllmessage.getMessage(mlid);
                    //有留言
                    if (models != null)
                    {
                        //绑定留言回复数据

                        rpComment.DataSource = models;
                        rpComment.DataBind();

                        
                    }
                    //没有留言内容的处理
                    else
                    {

                    }
                }
                //没有该数据信息
                else
                {
                    MessageBox.Show(this, "没有数据");
                }
            }
            else
            {
                //querystring中的id为空的时候则跳到主页
                Response.Redirect("index.aspx");
            }
    }
}